import 'package:enter_prise_web/page/index/index_controller.dart';
import 'package:enter_prise_web/page/settings/setting_controller.dart';
import 'package:enter_prise_web/util/MyDio.dart';
import 'package:enter_prise_web/widgets/page/page_controller.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class MyTableDataSource extends DataTableSource {
  final dynamic data;
  final int selectedCount;
  final String? id;

  final _sController = Get.find<IndexController>();
  final PageControllers pageController = Get.find();
  final MyDio dio = MyDio();

  MyTableDataSource(this.selectedCount, this.data, {this.id});

  @override
  DataRow? getRow(int index) {
    if (index > data.length) {
      return null;
    }

    List<DataCell> cells = [];

    data[index].values.forEach((element) {
      element is bool
          ? const SizedBox.shrink()
          : cells.add(DataCell(Text(
              element.toString(),
              textAlign: TextAlign.center,
            )));
    });

    cells.add(DataCell(Padding(
      padding: const EdgeInsets.only(right: 8.0),
      child: Row(
        children: [
          IconButton(
            highlightColor: Colors.transparent,
            splashColor: Colors.transparent,
            tooltip: "删除",
            onPressed: () async {
              showCupertinoDialog(
                  context: Get.context!,
                  builder: (c) {
                    return CupertinoAlertDialog(
                        title: const Text("删除"),
                        content: const Text("确定删除吗？"),
                        actions: [
                          CupertinoDialogAction(
                              child: const Text("取消"),
                              onPressed: () {
                                Get.back();
                              }),
                          CupertinoDialogAction(
                              child: const Text("确定"),
                              onPressed: () async {
                                if (id == "uid") {
                                  //用户
                                } else if (id == "gid") {
                                  //组
                                } else if (id == "oid") {
                                  //工件
                                  final settingController sController =
                                      Get.find<settingController>();
                                  sController.deleteById(index);
                                }
                                Get.back();
                              })
                        ]);
                  });
            },
            icon: const Icon(Icons.delete),
          ),
          IconButton(
            highlightColor: Colors.transparent,
            splashColor: Colors.transparent,
            tooltip: "编辑",
            onPressed: () {
              Fluttertoast.showToast(msg: "修改成功");
            },
            icon: const Icon(Icons.edit),
          ),
        ],
      ),
    )));
    return DataRow.byIndex(
        onSelectChanged: (value) {
          if (value!) {}
          data[index]["selected"] = value;
          if (value) {
            _sController.selectCount++;
          } else {
            _sController.selectCount--;
          }
          notifyListeners();
        },
        selected:
            data[index]['selected'] == null ? false : data[index]["selected"],
        cells: cells,
        index: index);
  }

  @override
  // TODO: implement isRowCountApproximate
  bool get isRowCountApproximate => false;

  @override
  // TODO: implement rowCount
  int get rowCount => data.length;

  @override
  // TODO: implement selectedRowCount
  int get selectedRowCount => 0;
}
